package cz.boxdesign.library.entity.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Component;

import cz.boxdesign.library.entity.Address;
import cz.boxdesign.library.entity.User;
import cz.boxdesign.library.entity.dao.query.UserSqlQuery;

@Component(value = "userDao")
public class UserDaoImpl extends EntityDaoAbstract<User, UserSqlQuery> {

	@Override
	protected void getEntityList(List<User> result, ResultSet rs) throws SQLException {
		while (rs.next()) {
			User u = getEntity(rs);
			result.add(u);
		}
	}

	/**
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	protected User getEntity(ResultSet rs) throws SQLException {
		User u = new User();
		Address a = new Address(rs.getString("ULICE_U"), rs.getString("MESTO_U"), rs.getString("PSC_U"));
		u.setAdress(a);
		u.setId(rs.getInt("ID"));
		u.setJmeno(rs.getString("JM_U"));
		u.setPrijmeni(rs.getString("PRIJMENI_U"));
		u.setLogin(rs.getString("LOGIN_U"));
		u.setPassword(rs.getString("PASS_U"));
		u.setAdmin(rs.getBoolean("IS_ADMIN"));
		u.setPlat(rs.getDouble("PLAT"));
		u.setRodneCislo(rs.getString("RODNE_CISLO"));
		u.setZamestanOd(rs.getDate("ZAMESTNAN_OD", Calendar.getInstance()));
		u.setZamestanDo(rs.getDate("ZAMESTNAN_DO", Calendar.getInstance()));
		u.setDatumRegistrace(rs.getDate("DATUM_REGISTRACE", Calendar.getInstance()));
		u.setObdobiDo(rs.getDate("OBDOBI_DO", Calendar.getInstance()));
		u.setObdobiOd(rs.getDate("OBDOBI_OD", Calendar.getInstance()));
		return u;
	}

	@Override
	protected String getSqlQuery(int queryValue) {
		return UserSqlQuery.getByValue(queryValue).getQuery();
	}

	@Override
	protected void fillStatement(User entity, int queryValue, PreparedStatement ps) throws SQLException {
		UserSqlQuery clause = UserSqlQuery.getByValue(queryValue);
		switch (clause) {
		case INSERT: {
			ps.setString(1, entity.getJmeno());
			ps.setString(2, entity.getPrijmeni());
			ps.setString(3, entity.getLogin());
			ps.setString(4, entity.getPassword());
			ps.setBoolean(5, entity.isAdmin());
			ps.setString(6, entity.getAdress().getUlice());
			ps.setString(7, entity.getAdress().getMesto());
			ps.setString(8, entity.getAdress().getPsc());
			ps.setDate(9, entity.getDatumRegistrace(), Calendar.getInstance());
			if (entity.isAdmin()) {
				ps.setNull(10, java.sql.Types.DATE);
				ps.setNull(11, java.sql.Types.DATE);
				ps.setDouble(12, entity.getPlat());
				ps.setString(13, entity.getRodneCislo());
				ps.setDate(14, entity.getZamestanOd(), Calendar.getInstance());
				ps.setDate(15, entity.getZamestanDo(), Calendar.getInstance());
			} else {
				ps.setDate(10, entity.getObdobiOd(), Calendar.getInstance());
				ps.setDate(11, entity.getObdobiDo(), Calendar.getInstance());
				ps.setDouble(12, 0d);
				ps.setNull(13, java.sql.Types.DATE);
				ps.setNull(14, java.sql.Types.DATE);
				ps.setNull(15, java.sql.Types.DATE);
			}
			break;
		}
		case UPDATE: {
			ps.setString(1, entity.getPassword());
			ps.setString(2, entity.getAdress().getUlice());
			ps.setString(3, entity.getAdress().getMesto());
			ps.setString(4, entity.getAdress().getPsc());
			ps.setDouble(5, entity.getPlat());
			ps.setInt(6, entity.getId());
			ps.setString(7, entity.getLogin());
			break;
		}
		case DELETE: {
			ps.setString(2, entity.getLogin());
			ps.setInt(1, entity.getId());
			break;
		}
		default:
			throw new IllegalArgumentException();
		}
	}

}
